﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.WindowsMobile.Samples.Location;

namespace Microsoft.WindowsMobile.Samples.Location
{
    public static class GpsPositionExtensions
    {
        public static string GetLongitude(this GpsPosition position, string format)
        {  
           return string.Format(
                format,
                position.LongitudeInDegreesMinutesSeconds.Degrees,
                position.LongitudeInDegreesMinutesSeconds.Minutes,
                position.LongitudeInDegreesMinutesSeconds.Seconds,
                (position.LongitudeInDegreesMinutesSeconds.IsPositive) ? "E" : "W");
        }

        public static string GetLatitude(this GpsPosition position, string format)
        {
            return string.Format(
                  format,
                  position.LatitudeInDegreesMinutesSeconds.Degrees,
                  position.LatitudeInDegreesMinutesSeconds.Minutes,
                  position.LatitudeInDegreesMinutesSeconds.Seconds,
                  (position.LatitudeInDegreesMinutesSeconds.IsPositive) ? "N" : "S");
        }

        public static string GetSatelitesInfo(this GpsPosition position)
        {
            Satellite[] satelites = position.GetSatellitesInView();
            string satellitesInViewCount = string.Empty;

            if (satelites == null)
            {
                satellitesInViewCount = "0";
            }
            else
            {
                satellitesInViewCount = satelites.Length.ToString();
            }

            return position.SatelliteCount.ToString() + "/" + satellitesInViewCount;
        }

        public static bool SatelliteIsInSolution(this GpsPosition _position, Satellite _satellite)
        {
            return _position.GetSatellitesInSolution().Any(satellite => satellite.Id == _satellite.Id);
        }
    }
}
